package com.pink;

import java.awt.*;
import java.io.*;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermissions;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Analysis implements InputTaskOutputLog {

    private RandomAccessFile outputFile;
    private List<String[]> r;

    //    taskFile 较小　使用的是项目路径　建议使用项目绝对路径
//    logFile 较大　使用的是本机路径　建议使用本机绝对路径
    public Analysis(String logFile) throws FileNotFoundException {
//        String t = Analysis.class.getResource(taskFile).getFile();
//        inputFile = new RandomAccessFile(t,"r");
        outputFile = new RandomAccessFile(logFile, "rw");
        r = new ArrayList<>();
    }

    public Analysis() throws FileNotFoundException {
//        String logFile = "/home/xieweig/document/snapshot.log";
        this("/home/xieweig/document/snapshot.log");
    }

    public List<String[]> getLists(String file) {
        String t = Analysis.class.getResource(file).getFile();
        RandomAccessFile inputFile = null;
        try {
            inputFile = new RandomAccessFile(t, "r");
            String content;
            while (true) {
                try {
                    content = inputFile.readLine();
                    if (content == null) break;
//                    核心解析规则代码
                    analysisCore(content);

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        } catch (FileNotFoundException e) {
            System.out.println("任务文件不存在");
            this.append("管理员提示　本文件不存在无法完成读取用户名密码读取任务");
        } finally {
            try {
                inputFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return r;
    }

    //    解析的核心规则 也可以专门扩展成一个类　类的构造函数中有ｒ也就是最终结果集的桥梁　主接口方法有入参是一行内容
    private void analysisCore(String content) {
        if (content.startsWith("#")) return;
        String[] split = content.split(":");
        if (split.length != 7) {
            System.out.println("任务数据不合法跳过此条"+Arrays.toString(split));
            return;
        }
        r.add(split);

    }

    public void append(Object content) {
        try {
            outputFile.seek(outputFile.length());
            String s = "[+++" + LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME) +
                    "+++] 用户：　" + content + System.lineSeparator();
            outputFile.write(s.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void clearOutput() {
//        outputFile.seek(0);
    }


    @Override
    public void close() throws IOException {
        if (outputFile != null) outputFile.close();
    }
//    public void Analysis

//todo ｍａｉｎ

    public static void main(String[] args) throws IOException, AWTException, InterruptedException {
        Path log = Paths.get("/home/xieweig/document/snapshot.log");
        if (Files.notExists(log)) Files.createFile(log,
                PosixFilePermissions.asFileAttribute(
                        PosixFilePermissions.fromString("rwxrwxr--")));
//        先分析数据　再做模拟运动　后者持有前者的引用　，越靠后越要后来居上　拥有前者的引用
        Analysis analysis = new Analysis(log.toString());
        RobotEx robotEx = new RobotEx();
        WoodWorker woodWorker = new WoodWorker(robotEx, analysis);
        System.out.println("start");
        String[] tasks = {"/task.txt"};
        for (int i = 0; i < tasks.length; i++) {

            List<String[]> lists = analysis.getLists(tasks[i]);
            System.out.println(lists.size());
            for (int j = 0; j < lists.size(); j++) {

                woodWorker.oneDragen(lists.get(j));
            }
        }
//        for (int i = 0; i < lists.size(); i++) {
////            WoodWorker woodWorker = new WoodWorker(robotEx,lists.get(i));
//            WoodWorker woodWorker = new WoodWorker(robotEx, analysis,lists.get(i));
//            woodWorker.oneDragen();
//        }

    }

    public static void main1(String[] args) {
        List<String[]> results = new LinkedList<>();
        try {
            String task = Analysis.class.getResource("/task.txt").getFile();
            RandomAccessFile file = new RandomAccessFile(task, "r");
            while (true) {

                String s = file.readLine();
                System.out.println(s);
                if (s == null || s.equals("")) {
                    System.out.println("jieshule");
                    break;
                }


                if (s.startsWith("#")) continue;
                final String[] content = s.split(":");
//                if (content.length==4){
//                    results.add(content);
//                }
                if (content.length != 4) continue;

                results.add(content);


            }

            file.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("end");
        results.forEach((x) -> {
            System.out.println(Arrays.toString(x));
        });
    }
}
